package cn.yangphere.web.action.product;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;

import cn.yangphere.bean.product.ProductType;
import cn.yangphere.service.product.ProductTypeService;
import cn.yangphere.web.formbean.product.ProductTypeForm;

@Controller("/control/product/type/manage")
public class ProductTypeManageAction extends DispatchAction {

	@Resource(name = "productTypeServiceBean")
	private ProductTypeService productTypeService;

	/**
	 * 查询界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("query");
	}
	
	/**
	 * 类别添加界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}
	
	/**
	 * 类别添加
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductTypeForm formBean = (ProductTypeForm) form;
		ProductType type = new ProductType(formBean.getName(),formBean.getNote());
		if(formBean.getParentId()!=null && formBean.getParentId()>0){
			type.setParent(new ProductType(formBean.getParentId()));
		}
		productTypeService.save(type);
		request.setAttribute("message", "添加类别成功");
		return mapping.findForward("message");
	}

	/**
	 * 类别修改界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductTypeForm formBean = (ProductTypeForm) form;
		ProductType type = productTypeService.find(ProductType.class, formBean.getTypeid());
		formBean.setName(type.getName());
		formBean.setNote(type.getNote());
		return mapping.findForward("edit");
	}
	
	/**
	 * 类别修改
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductTypeForm formBean = (ProductTypeForm) form;
		ProductType type = productTypeService.find(ProductType.class, formBean.getTypeid());
		type.setName(formBean.getName());
		type.setNote(formBean.getNote());
		productTypeService.update(type);
		request.setAttribute("message", "修改类别成功");
		return mapping.findForward("message");
	}
}
